//
//  MZYPictureViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYPictureViewController.h"
#import "MZYCollectionViewCell.h"
#import "MZYHeader.h"
#import "MZYsecViewController.h"
@interface MZYPictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, assign)NSInteger page;





@end

@implementation MZYPictureViewController

- (void)dealloc {
    [_collectionView release];
    [_arr release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self createData];
    self.title = @"图鉴";
    
}


- (void)createTableView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT / 2 - 54);
//    flowLayout.minimumInteritemSpacing = 10;
//    flowLayout.minimumLineSpacing = 10;
//    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 20, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 54) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[MZYCollectionViewCell class] forCellWithReuseIdentifier:@"reuser"];
    
    // 建立小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.detailsLabelText = @"请稍后";
    self.HUD.dimBackground = YES;

    
//    //下拉
//    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
//    //上啦
//    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
//    
//    self.page = 1;

    
    
    
}
- (void)createData {
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager GET:@"http://api.ttime.com/v2/productActivityApi/getProductActivityDetails?activity_id=15&token=92c06c9f918d3185b4b78110996bcd2b&v=2.4.0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//       
//        NSMutableDictionary *dic = (NSMutableDictionary *)responseObject;
//        NSArray *mArr = dic[@"result"][@"products"];
//        self.arr = [NSMutableArray array];
//        for (NSDictionary *tempDic in mArr) {
//            MZYPictureModel *pic = [[MZYPictureModel alloc] init];
//            [pic setValuesForKeysWithDictionary:tempDic];
//            [self.arr addObject:pic];
//            [pic release];
//        }
//        
//        if (self.arr.count > 0) {
//            [self.HUD removeFromSuperview];
//            [self.collectionView reloadData];
//        }
//        [self.collectionView.header endRefreshing];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
    
    NSURL *url = [NSURL URLWithString:@"http://api.ttime.com/v2/productActivityApi/getProductActivityDetails?activity_id=15&token=92c06c9f918d3185b4b78110996bcd2b&v=2.4.0"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            
            
            NSMutableDictionary *dic = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            
            
            NSArray *mArr = dic[@"result"][@"products"];
                    self.arr = [NSMutableArray array];
                    for (NSDictionary *tempDic in mArr) {
                        MZYPictureModel *pic = [[MZYPictureModel alloc] init];
                        [pic setValuesForKeysWithDictionary:tempDic];
                        [self.arr addObject:pic];
                        [pic release];
                    }
            
                    if (self.arr.count > 0) {
                        [self.HUD removeFromSuperview];
                        [self.collectionView reloadData];
                    }
                    [self.collectionView.header endRefreshing];
        }
        
    }];
    
    NSURLCache *cache = [NSURLCache sharedURLCache];
    NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
    if (response) {
        NSLog(@"源这个请求已存在缓存");
    } else {
        NSLog(@"源这个请求没有缓存");
    }


}

#pragma mark 上拉下拉方法
//下拉
//- (void)header {
//    //把已经刷新的数据删除,否则越刷新重复数据越多
//    [self.InfoArr removeAllObjects];
//    [self createData];
//    [self.collectionView.header endRefreshing];
//}
//上拉
//-(void)footer {
//    self.page ++;
//    NSLog(@"%ld",(long)self.page);
//    NSString *url = [NSString stringWithFormat:@"http://api.ttime.com/v2/productActivityApi/getProductActivityDetails?activity_id=%ld&token=92c06c9f918d3185b4b78110996bcd2b&v=2.4.0", (long)self.page];
//    
//    
//    // 解析UITableView
//    AFHTTPRequestOperationManager *Tabmanager = [AFHTTPRequestOperationManager manager];
//    [Tabmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSMutableDictionary *dic = (NSMutableDictionary *)responseObject;
//        NSArray *mArr = dic[@"result"][@"products"];
//        self.arr = [NSMutableArray array];
//        for (NSDictionary *tempDic in mArr) {
//            MZYPictureModel *pic = [[MZYPictureModel alloc] init];
//            [pic setValuesForKeysWithDictionary:tempDic];
//            [self.arr addObject:pic];
//            [pic release];
//        }
//        if (self.arr.count > 0) {
//                        self.HUD.hidden = YES;
//            [self.collectionView reloadData];
//        }
//        
//        [self.collectionView.footer endRefreshing];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
//}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MZYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
    MZYPictureModel *pic = self.arr[indexPath.row];
    cell.myLabel.text = pic.name;
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:pic.gallary[0]] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击我");
    MZYsecViewController *secVC = [[MZYsecViewController alloc] init];
    MZYPictureModel *model = self.arr[indexPath.row];
    secVC.hidesBottomBarWhenPushed = YES;
    secVC.product_id = model.product_id;
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
